import {
	postUserLoginToken,
	deleteUserLoginToken
} from '@/services/user.service'

import {
	App_Version,
	App_Type
} from "@/config/env.js"

const showModal = function(content, code, config) {
	if (config && config.isCloseAuthShowShowModal) {
		return false
	}
	uni.showModal({
		title: '温馨提示：' +( code|| '' ),
		content,
		showCancel: false,
		success: function(res) {
			if (code === 20009) { // 需要实名认证
				// #ifdef  APP-PLUS
				uni.reLaunch({
					url: '/pages/auth/index'
				})
				// #endif
				// #ifdef  H5
				uni.redirectTo({
					url: '/pages/auth/index'
				})
				// #endif
				uni.hideLoading()
			} else if (code === 20010) {
				uni.navigateTo({
					url: '/pages/account/index'
				})
				uni.hideLoading()
			} else if (code + '' === '9001') {
				uni.navigateTo({
					url: '/pages/mine/downApk/index'
				})
				uni.hideLoading()
			}
		}
	})
}

const request = function(url, method, data = {}, config = {}, isLoading = false) {
	if(App_Version && App_Type){
		data.version = App_Version
		data.app = App_Type
	}
	
	let options = {
		url,
		method,
		data,
		dataType: 'json'
	}
	if (postUserLoginToken()) {
		options.header = {
			'Authorization': 'Bearer ' + postUserLoginToken()
		}
	}

	if(isLoading){
		uni.showToast({
			title: '加载中...',
			icon: 'loading',
			duration: 10000,
			mask: true
		});
	}
	
	return new Promise((resolve, reject) => {
		uni.request({
			...options,
			...config,
			success: (res) => {
				const {
					statusCode,
					data
				} = res
				if(isLoading){
					uni.hideToast()
				}
				if (statusCode === 200 || statusCode === 304) {
					const {
						code,
						msg = ''
					} = data
					if (code + '' === '200') {
						return resolve(data)
					}
					if (code + '' === '401') {
						// todo 未登录跳转至登录页面，并且记录当前的页面
						deleteUserLoginToken()
						// #ifdef  APP-PLUS
						uni.reLaunch({
							url: '/pages/login/login/index'
						})
						// #endif
						// #ifdef  H5
						uni.redirectTo({
							url: '/pages/login/login/index'
						})
						window.location.reload()
						// #endif
						uni.hideLoading()
						return reject({})
					}
					if (code + '' === '403') {
						// to 没有权限
						uni.hideLoading()
						return reject(data)
					}
					if (code + '' === '404') {
						// 理论上不会有这种情况出现才合理
						uni.hideLoading()
						return reject(code)
					}
					
					console.log('showModal', res)
					console.log('opts', options )
					showModal(msg, code, config)
					return reject(data)
				} else {
					console.log(res)
					uni.hideLoading()
					return reject(statusCode)
				}
			},
			fail(err) {
				const {
					message,
					code
				} = err
				uni.hideLoading()
				uni.hideToast()
				showModal(message || '服务器繁忙，请稍后重试', code)
				return reject(err)
			}
		})
	})
}

export default class http {
	constructor() {}
	static get(url, data, config, isLoading) {
		return request(url, 'GET', data, config, isLoading)
	}
	static post(url, data, config, isLoading) {
		return request(url, 'POST', data, config, isLoading)
	}
	static put(url, data, config, isLoading) {
		return request(url, 'PUT', data, config, isLoading)
	}
	static delete(url, data, config, isLoading) {
		return request(url, 'DELETE', data, config, isLoading)
	}
}
